var ReservationsInfoDlg = {reservationsInfoData: {}, zTreeInstance: null, validateFields: {}};//初始化市场预约详情对话框
//清除数据
ReservationsInfoDlg.clearData = function () {
    this.reservationsInfoData = {};
};
// 设置对话框中的数据 @param key 数据的名称 @param val 数据的具体值
ReservationsInfoDlg.set = function (key, val) {
    this.reservationsInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
};
//设置对话框中的数据 @param key 数据的名称 @param val 数据的具体值
ReservationsInfoDlg.get = function (key) {
    return $("#" + key).val();
};
//关闭此对话框
ReservationsInfoDlg.close = function () {
    parent.layer.close(parent.layer.getFrameIndex(window.name));
};
//收集数据
ReservationsInfoDlg.collectData = function () {
    this.set('id').set('customerName').set('customerPhone').set('customerPhone2').set('customerSex').set('customerAge').set('economicAbility').set('address').set('consumAbility').set('consumptionHabits')
        .set('qqNumber').set('wechatNumber').set('occupation').set('archiving').set('filingTime').set('statuss').set('maritalStatus').set('isHaveChildren').set('characters')
        .set('entryClerk').set('obstaclePoint').set('oneChannel').set('twoChannel').set('thrChannel').set('oneMedium').set('twoMedium').set('thrMedium').set('appointmentArrivalTime').set('appointmentPayService')
        .set('isLarge').set('keyWord').set('department').set('followers').set('content').set('reservationsNum').set('otherContact').set('tcName').set('doctor')
        .set('accessDisease').set('remarks').set('classification').set('status').set('outpatientName').set('phoneSharShip').set('userName');
};
//验证数据是否为空
ReservationsInfoDlg.validate = function () {
    $('#reservationsInfoForm').data("bootstrapValidator").resetForm();
    $('#reservationsInfoForm').bootstrapValidator('validate');
    return $("#reservationsInfoForm").data('bootstrapValidator').isValid();
};
//提交添加预约客户
ReservationsInfoDlg.addSubmit = function (obj) {
    this.clearData();
    this.collectData();
    var appointmentPayService = $('#appointmentPayService').val();
    var customerSex = $('input[name="sex"]:checked').val();
    var customerName = $("#customerName").val(); //姓名
    var customerPhone = $("#customerPhone").val(); //电话
    var appointmentArrivalTime = $("#appointmentArrivalTime").val(); //预约时间
    var outpatientName = $("#outpatientName").val(); //门诊
    var oneChannel = $("#oneChannel").val();
    var twoChannel = $("#twoChannel").val();
    var thrChannel = $("#thrChannel").val();
    var accessDisease = $("#accessDisease").val();
    var statuss = $("#statuss").val();
    var remarks = $("#remarks").val();
    var customerAge = $("#customerAge").val();
    if (!this.validate()) {
        return;
    }
    if (customerName == null || customerName == "") {
        alert("请输入姓名！");
        return;
    }
    if (customerPhone == null || customerPhone == "") {
        alert("请输入电话！");
        return;
    }
    if (customerPhone.length > 0 && customerPhone.length < 7 || customerPhone.length > 8 && customerPhone.length < 11) {
        alert("当前输入号码格式不对，请重新输入！");
        return;
    }
    if (statuss == null || statuss == "") {
        alert("请选择状态！");
        return;
    }
    if (appointmentPayService == null || appointmentPayService == "") {
        alert("请选择预约项目！");
        return;
    }
    if (appointmentArrivalTime == null || appointmentArrivalTime == "") {
        alert("请选择预约时间！");
        return;
    }
    if (outpatientName == null || outpatientName == "") {
        alert("请选择门诊！");
        return;
    }
    if (oneChannel == null || oneChannel == "请选择") {
        alert("请选择一级渠道！");
        return;
    }
    if (twoChannel == null || twoChannel == "请选择") {
        alert("请选择二级渠道！");
        return;
    }
    if (thrChannel == null || thrChannel == "请选择") {
        alert("请选择三级渠道！");
        return;
    }
    if (oneChannel != "直接上门" && accessDisease == "") {
        alert("请选择访问病种！");
        return;
    }
    if ($('#customerAge').val().length > 3) {
        alert("年龄长度应小于3")
        return;
    }
    if (remarks == null || remarks == "") {
        alert("填写客户详情！");
        return;
    }
    this.reservationsInfoData["customerSex"] = customerSex;
    var ajax = new $ax(Feng.ctxPath + "/reservation/add", function (data) {
        if (data.split("#")[0] == 1) {
            if (obj == '1') {
                Feng.success("添加成功!");
                window.parent.ReservationsRecordTable.search();
                ReservationsInfoDlg.close()
            }else if (obj == '2'){
                window.parent.document.getElementById('reservationsId').value=data.split("#")[1];
                window.parent.document.getElementById('customerName').innerHTML=customerName;
                window.parent.document.getElementById('customerAge').innerHTML=customerAge;
                window.parent.document.getElementById('customerPhone').innerHTML=customerPhone;
                window.parent.document.getElementById('customerSex').innerHTML=customerSex;
                window.parent.document.getElementById('reach').value=customerName;
                Feng.success("添加成功!");
                parent.layer.close(parent.layer.getFrameIndex(window.name));
            }else {
                Feng.success("添加成功!");
                window.location.href = Feng.ctxPath + "/weixin/index";
            }
        } else {
            alert("客户电话已存在");
        }
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.reservationsInfoData);
    ajax.start();
};
//微信返回
ReservationsInfoDlg.patient = function () {
    if ($("#numValue").val() == '1') {
        window.location.href = Feng.ctxPath + "/weixin/index";
    } else if ($("#numValue").val() == '2') {
        window.location.href = Feng.ctxPath + "/reservation/market_patient";
    }
};
//初始化数据
ReservationsInfoDlg.initData = function () {
    var seletecustomerSex = $('#customerSexValue').val();
    if ($('#seletecustomerSex').val() != "") {
        $("#customerSex option[value='" + seletecustomerSex + "']").attr("selected", "selected");
    }
    if ($('#seletecustomerSex').val() != "") {
        $("#select1 option[value='" + seletecustomerSex + "']").attr("selected", "selected");
    }
    var seletemaritalStatus = $('#maritalStatusValue').val();
    if ($('#seletemaritalStatus').val() != "") {
        $("#maritalStatus option[value='" + seletemaritalStatus + "']").attr("selected", "selected");
    }
    var seleteisLarge = $('#isLargeValue').val();
    if ($('#seleteisLarge').val() != "") {
        $("#isLarge option[value='" + seleteisLarge + "']").attr("selected", "selected");
    }
    var seletestatus = $('#statusValue').val();
    if ($('#seletestatus').val() != "") {
        $("#status option[value='" + seletestatus + "']").attr("selected", "selected");
    }
    var seletephoneSharShip = $('#phoneSharShipValue').val();
    if ($('#seletephoneSharShip').val() != "" && seletephoneSharShip != "") {
        $("#phoneSharShip option[value='" + seletephoneSharShip + "']").attr("selected", "selected");
    }
    var seleteaccessDisease = $('#accessDiseaseValue').val();
    if ($('#seleteaccessDisease').val() != "" && seleteaccessDisease != "") {
        $("#accessDisease option[value='" + seleteaccessDisease + "']").attr("selected", "selected");
    }
};

function checkDate() {
    var date = new Date();
    date.setDate(date.getDate() - 1);
    var now = date.valueOf();
    var time = new Date($("#appointmentArrivalTime").val()).valueOf();
    if (now > time) {
        layer.alert("预约时间不能小于当前时间！");
        $("#appointmentArrivalTime").val("")
    }
};

$(function () {
    Feng.initValidator("reservationsInfoForm", ReservationsInfoDlg.validateFields);
    ReservationsInfoDlg.initData();
    $(".modify").attr("readOnly", "true");
    $(".select_styles").attr("disabled", "disabled");
});